import {createRouter, createWebHistory} from "vue-router";
import AdminBase from "../pages/AdminBase.vue";
import AdminLogin from "../pages/AdminLogin.vue";
import StudentLogin from "../pages/StudentLogin.vue";
import {useStore} from "../store";
import {dayjs, ElMessage} from "element-plus";

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: AdminLogin },
    { path: '/stu-login', component: StudentLogin },
    { path: '/admin', component: AdminBase, children: [
        { path: '/admin', redirect: '/admin/home' },
        { path: '/admin/home', component: () => import('../pages/admin/Home.vue') },
        { path: '/admin/stu-list', component: () => import('../pages/admin/StudentList.vue') },
        { path: '/admin/department-list', component: () => import('../pages/admin/DepartmentList.vue') },
        { path: '/admin/classroom-list', component: () => import('../pages/admin/ClassroomList.vue') },
        { path: '/admin/course-list', component: () => import('../pages/admin/CourseList.vue') },
        { path: '/admin/condition', component: () => import('../pages/admin/CourseCondition.vue') },
        { path: '/admin/CourseAssignment', component: () => import('../pages/admin/CourseAssignmentPage.vue') },
        { path: '/admin/:pathMatch(.*)*', component: () => import('../pages/404.vue') },
    ]},
    { path: '/student', redirect:'/student/index', component: () => import('../pages/student/Home.vue'), children: [
        { path: '/student/index', component: () => import('../pages/student/StudentIndex.vue') },
        { path: '/student/my-course', component: () => import('../pages/student/StudentCourse.vue') },
        { path: '/student/:pathMatch(.*)*', component: () => import('../pages/404.vue') },
    ]},
    { path: '/:pathMatch(.*)*', component: () => import('../pages/404.vue') },
]

const router = createRouter({
    history: createWebHistory(),
    routes,
})



//路由守卫
router.beforeEach((to, from, next) => {

    console.log(to.path)
    console.log(to.path.startsWith('/admin'))

    if (to.path === '/login' || to.path === '/stu-login') {
        return next()
    }

    //获取pinia中的信息
    const store = useStore();
    //如果是/admin路径下的路由
    if (to.path.startsWith('/admin')) {
        let current = dayjs();
        //获取上次登录时间
        let timeIsOk = dayjs(store.adminInfo.loginTime, 'YYYY-MM-DD HH:mm:ss').isValid();
        console.log(timeIsOk)
        console.log(store.adminInfo)
        if (store.adminIsLogin && timeIsOk){
            //上次登录时间是两小时前 则需要重新登录
            if (current.diff(dayjs(store.adminInfo.loginTime, 'YYYY-MM-DD HH:mm:ss'), 'hour') > 2) {
                store.adminIsLogin = false;
                ElMessage.warning('登录过期，请重新登录')
                return next('/login')

            } else {
                return next()
            }
        }

        ElMessage.warning('请先登录')
        return next('/login')
    }

    //如果是/student路径下的路由
    if (to.path.startsWith('/student')) {
        let current = dayjs();
        //获取上次登录时间
        let timeIsOk = dayjs(store.studentInfo.loginTime, 'YYYY-MM-DD HH:mm:ss').isValid();
        console.log(timeIsOk)
        console.log(store.studentInfo)
        if (store.studentIsLogin && timeIsOk){
            //上次登录时间是两小时前 则需要重新登录
            if (current.diff(dayjs(store.studentInfo.loginTime, 'YYYY-MM-DD HH:mm:ss'), 'hour') > 2){
                store.studentIsLogin = false;
                ElMessage.warning('登录过期，请重新登录')
                return next('/stu-login')
            }else {
                return next()
            }
        }else {
            ElMessage.warning('请先登录')
            return next('/stu-login')
        }
    }


})

export default router;